/*
 * @Author: fisher
 * @Date: 2023-11-13 11:08:10
 * @LastEditTime: 2023-11-13 17:05:56
 * @LastEditors: fisher
 * @Description:
 * @FilePath: \FCT\src\utils\FCT\measure\Area.js
 */
import { screen2degrees } from "../utils.js";
import { lineString, length } from "@turf/turf";

export default (collection, handler, cb) => {
  let points = [];
  let elevation = 10;
  handler.setInputAction((e) => {
    // 当确定第一个点位后开启动态绘制线体，新增移动事件
    if (points.length == 2) {
      // 添加鼠标移动事件
      handler.setInputAction((e) => {
        let tempPoint = screen2degrees(e.endPosition);
        points.length = points.length - 1;
        points.push([tempPoint.x, tempPoint.y, elevation]);
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }
    const p = screen2degrees(e.position);
    points.push([p.x, p.y, elevation]);
    // 点下第一个点后开始绘制
    addPoint(collection, [p.x, p.y, elevation]);
    console.log("points", points);
    console.log("points.length", points.length);

    if (points.length == 3) {
      addDynamicPolygon(collection, points);
    }
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

  handler.setInputAction(() => {
    // 删掉最后一组未确认的点
    points.length = points.length - 1;
    // 右键清除点击事件
    clearHandlerInput(handler);
    // 删除动态线，替换为固定线
    collection.entities.removeById("dynamicPolygon");
    addPolygon(collection, points, elevation);
    // 计算长度
    const result = calculateDistance(points);
    // 结果返回回调函数
    cb(result);
  }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
};

// 清除鼠标事件
const clearHandlerInput = (handler) => {
  handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
};

// 添加点位
const addPoint = (collection, position) => {
  collection.entities.add(
    new Cesium.Entity({
      position: Cesium.Cartesian3.fromDegrees(
        position[0],
        position[1],
        position[2]
      ),
      point: {
        color: Cesium.Color.RED,
        pixelSize: 10,
      },
    })
  );
};

// 添加线
const addPolygon = (collection, points) => {
  collection.entities.add(
    new Cesium.Entity({
      polygon: {
        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights(points.flat(1)),
        perPositionHeight: true,
        height: 1,
        material: Cesium.Color.BLUE,
      },
    })
  );
};
//绘制动态线
const addDynamicPolygon = (collection, points) => {
  const data = points.flat(1);
  console.log("data", data);
  collection.entities.add(
    new Cesium.Entity({
      id: "dynamicPolygon",
      polygon: {
        // 这个方法上面有重点说明
        hierarchy: new Cesium.CallbackProperty(() => {
          return Cesium.Cartesian3.fromDegreesArrayHeights(data);
        }, false),
        perPositionHeight: true,
        // 线的颜色
        material: Cesium.Color.BLUE,
      },
    })
  );
};
const calculateDistance = (data) => {
  let result = "";
  const line = lineString(data);
  const len = length(line, { units: "meters" });
  // 结果处理
  if (len > 1000) {
    // toFiexd方法，保留小数点
    result = (len / 1000).toFixed(2) + "千米";
  } else {
    result = len.toFixed(2) + "米";
  }
  return result;
};
